<?php
require_once('../model/m_shifts.php');

class maintenance_shifts{

	public $data = array();
	public $idshift_schedule;
	public $name;
	public $start_time;
	public $end_time;
	public $description;
	
	public function actionView(){		
		$this->data = getShifts(); //get all shifts	
		include_once("../views/v_session.php");		
		include_once("../views/v_header_principal_lev1.html");
		include_once("../views/v_maintenance_shifts.html");
		include_once("../views/v_footer_principal.html");	
	}
	
	public function actionAdd(){
		include_once("../views/v_session.php");	
		include_once("../views/v_header_principal_lev1.html");
		include_once("../views/v_maintenance_shifts_add.html");
		include_once("../views/v_footer_principal.html");
	}

	public function actionAddShift(){	
		if(addShift($this->name,
				   $this->start_time,
				   $this->end_time,
				   $this->description)){
			header("location:c_maintenance_shifts.php?view=list");
			exit;				
		} else {		
			echo "<h1> Error creating Shift</h1>";
			exit;
		}	
	}
	
	public function actionEditShift($shiftid){		
		$current_shift = array();
		$current_shift = getShift($shiftid);
		
		///*** FALTA MANEJAR ROLE ID INEXISTENTE
		include_once("../views/v_session.php");		
		include_once("../views/v_header_principal_lev1.html");
		include_once("../views/v_maintenance_shifts_edit.html");
		include_once("../views/v_footer_principal.html");
	}	
	
	public function actionSaveShiftEdit(){
		
		if(saveShiftEdit($this->idshift_schedule,
						 $this->name,
						 $this->start_time,
						 $this->end_time,
						 $this->description)){
			header("location:c_maintenance_shifts.php?view=list");
			exit;
		}
		else {
			include_once("../views/v_session.php");		
			include_once("../views/v_header_principal_lev1.html");
			echo "<h1>THERE WAS A PROBLEM WHEN TRYING TO SAVE THE CHANGES!!!</h1>";
			include_once("../views/v_footer_principal.html");
		}	
	
	}	
	
	public function actionDeleteShift($shiftid){
		
		if(deleteShift($shiftid)){
			header("location:c_maintenance_shifts.php?view=list");
			exit;
		}
		else {
			include_once("../views/v_session.php");		
			include_once("../views/v_header_principal_lev1.html");
			echo "<h2>Unable to delete Shift. Make sure the shift is not in use by any user.</h2>";
			echo "<br><br>";
			echo '<input type="button" value="Back" onclick="goBack();"/>';
			include_once("../views/v_footer_principal.html");
		}	
	
	}	
}

$instancia= new maintenance_shifts;

if(isset($_GET['view'])){
	if($_GET['view']=="list")	
		$instancia->actionView();
}

if(isset($_GET['add'])){
	if($_GET['add']=="go")	
		$instancia->actionAdd();
}

if(isset($_GET['new_shift'])){
		$instancia->name = $_GET['new_shift'];
		$instancia->start_time = $_GET['start_time'];
		$instancia->end_time = $_GET['end_time'];
		$instancia->description = $_GET['description'];

		$instancia->actionAddShift();
}

if(isset($_GET['edit'])){
		$instancia->actionEditShift($_GET['edit']);
}

if(isset($_GET['save_shift'])){
		$instancia->idshift_schedule = $_GET['save_shift'];
		$instancia->name = $_GET['name'];
		$instancia->start_time = $_GET['start_time'];
		$instancia->end_time = $_GET['end_time'];
		$instancia->description = $_GET['description'];
		
		$instancia->actionSaveShiftEdit();
}

if(isset($_GET['delete'])){
		$instancia->actionDeleteShift($_GET['delete']);
}
?>